﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Potato.Model.Ent;
using OMG.Fill.Potato.Service;
using OMG.Fill.Potato.Services;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Potato.Model.UI.Controllers
{
    public class ReportController : ApiController
    {
        ReportServices reportServices = new ReportServices();
        
        //[ApiActionAuthorize("获取填报基本信息T_Report分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<ReportModel>> PostPageList(ReportQuery query)
        {
            return reportServices.GetPageList(query);
        }
        public Pager<List<ReportModel>> PostReviewPageList(ReportQuery query)
        {
            return reportServices.GetReviewPageList(query);
        }
        //[ApiActionAuthorize("新增填报基本信息T_Report指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(ReportEntity model) {
            return reportServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改填报基本信息T_Report指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(ReportEntity model)
        {
            return reportServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除填报基本信息T_Report指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return reportServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取填报基本信息T_Report指定对象操作")]
        //[ApiActionException]
        public ReportDataModel Get(string id,string reportCode)
        {
            return reportServices.GetReportData(id,reportCode);
        }
        public ResResult<int> PostUpStatus(ReviewModel model)
        {
            return reportServices.UpStatus(model.Id, model.Status, model.Opinion);
        }
        public ResResult<List<ReportReview>> GetReportReview(string AreaCode,string FillInYear) {
            return reportServices.GetReportReview(AreaCode, FillInYear);
        }
        //首页查看各地区填报情况类
        [HttpPost]
        public ResResult<List<ReportDetailReview>> GetDetailReportReview(ReportModel model)
        {
            return reportServices.GetDetailReportReview(model);
        }
        /// <summary>
        /// 模板下载响应
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetTemplateData(string ReportCode)
        {
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = "";
            switch (ReportCode)
            {
                case "PotPromotion":
                    strFileName = "（七）马铃薯技术推广机构及人员相关情况统计表.xls";//文件名称
                    break;
                case "PotYearProduct":
                    strFileName = "（一）马铃薯各品种全年生产情况统计表.xls";//文件名称
                    break;
                case "PotSeasonProduct1":
                    strFileName = "（二）大春-马铃薯生产情况统计表.xls";//文件名称
                    break;
                case "PotSeasonProduct2":
                    strFileName = "（二）秋作-马铃薯生产情况统计表.xls";//文件名称
                    break;
                case "PotSeasonProduct3":
                    strFileName = "（二）冬作-马铃薯生产情况统计表.xls";//文件名称
                    break;
                case "PotSeed":
                    strFileName = "（三）马铃薯种薯生产情况表.xls";//文件名称
                    break;
                case "PotProject":
                    strFileName = "（四）马铃薯项目实施情况统计表.xls";//文件名称
                    break;
                case "PotSaleStorege":
                    strFileName = "（五）马铃薯销售、存储与加工情况统计表.xls";//文件名称
                    break;
                case "PotProsecution":
                    strFileName = "（六）马铃薯生产经营实体及相关情况.xls";//文件名称
                    break;
            }
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(GetExcelTemplate(ReportCode).GetBuffer());
            curContext.Response.End();
        }

        public MemoryStream GetExcelTemplate(string ReportCode)
        {
            //MemoryStream ms = new MemoryStream();
            var workbook = new HSSFWorkbook();
            string fileIndex = HttpRuntime.AppDomainAppPath.ToString();
            string templateName = fileIndex + @"Excel\PotDownload.xls";//模板文件路径
            string sheetName = ReportCode;                       
            FileStream file = new FileStream(templateName, FileMode.Open, FileAccess.Read);//读模板文件
           HSSFWorkbook templateBook = new HSSFWorkbook(file);//读取模板文件信息转换成Excel工作薄
            HSSFSheet sheet = templateBook.GetSheet(sheetName) as HSSFSheet;//读取模板所在sheet位置
            sheet.CopyTo(workbook, "Sheet1", true, true);
            var tem = workbook.GetSheetAt(0);
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                file.Close();
                return ms;
            }

        }


    }

    
    public class ReviewModel
    {
        public string Id { get; set; }
        public string Status { get; set; }
        public string Opinion { get; set; }
    }
}